/**
 *  This code is a part of Quadratix project.
 * Copyright (C) 2009 - 2010 Valery A. Ilychev aka Sarutobi (ilychev at gmail dot com)
 * 
 *  This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
package ru.sarutobi.quadratix.ui;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

import ru.sarutobi.quadratix.Board;

public class BoardPanel extends JPanel  {
	Board board;

	Color current;
	Fishka[][] elems;
	boolean finished;
	
	public BoardPanel(){
		
	}
	
	public BoardPanel(Board board, ActionListener control) {
		super();
		setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
		this.board = board;
		elems = new Fishka[board.getSize()][board.getSize()];
		setLayout(new GridLayout(board.getSize(), board.getSize()));
		for (int y = 0; y < board.getSize(); y++)
			for (int x = 0; x < board.getSize(); x++) {
				Fishka f = new Fishka(x, y);
				f.addActionListener(control);
				elems[x][y] = f;
				add(f);
			}
		current = Color.WHITE;
		finished = true;
	}
	
	public void higlightMove(int pos, Color col){
		int x = pos % board.getSize();
		int y = pos / board.getSize();
		elems[x][y].setBackground(col);
	}

	public void higlightSquare(final int[] coords, final int target) {
		Thread t = new Thread() {
			public void run() {
				while(!finished){
					try{
						sleep(100);
					} catch (InterruptedException ie) {
						
					}
				}
				finished = false;
				try {
						int size = board.getSize();
						int x0 = coords[0] % size; int y0 = coords[0] / size;
						int x1 = coords[1] % size; int y1 = coords[1] / size;
						int x2 = coords[2] % size; int y2 = coords[2] / size;
						int x3 = coords[3] % size; int y3 = coords[3] / size;
					
					for (int i = 0; i < 4; i++) {	
						elems[x0][y0].setBackground(Color.LIGHT_GRAY);
						elems[x1][y1].setBackground(Color.LIGHT_GRAY);
						elems[x2][y2].setBackground(Color.LIGHT_GRAY);
						elems[x3][y3].setBackground(Color.LIGHT_GRAY);
						sleep(200);

						elems[x0][y0].setBackground(Color.DARK_GRAY);
						elems[x1][y1].setBackground(Color.DARK_GRAY);
						elems[x2][y2].setBackground(Color.DARK_GRAY);
						elems[x3][y3].setBackground(Color.DARK_GRAY);
						sleep(200);
					}
					Color col = (target == Board.WHITE) ? Color.WHITE : Color.BLACK;
										
					elems[x0][y0].setBackground(col);
					elems[x1][y1].setBackground(col);
					elems[x2][y2].setBackground(col);
					elems[x3][y3].setBackground(col);
					
					
				} catch (Exception e) {
				}
				finished = true;
			}
		};
		t.start();
		
	}

	public void visualize(int x, int y, int move) {
		Color col = (move == Board.WHITE) ? Color.white : Color.black;
		elems[x][y].setBackground(col);
		
	}
	
	public boolean isFinished(){
		return finished;
	}
}
